package com.example.yichuangyun.mvvm.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemTabBinding;
import com.example.yichuangyun.model.company.CompanyLabelModel;

import java.util.List;

public class InfoTabAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context context;
    private final List<CompanyLabelModel> data;
    private OnTabItemClickListener onTabItemClickListener;

    public InfoTabAdapter(Context context, List<CompanyLabelModel> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemTabBinding binding = ItemTabBinding.inflate(LayoutInflater.from(context), parent, false);
        return new MyViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            CompanyLabelModel model = data.get(position);
            myViewHolder.binding.tvTitle.setText(model.getCount() > 0 ? context.getString(R.string.tab_title, model.getTitle(), model.getCount()) : model.getTitle());
            myViewHolder.binding.line.setVisibility(model.isSelect() ? View.VISIBLE : View.INVISIBLE);
            myViewHolder.binding.tvTitle.setTextColor(model.isSelect() ? context.getColor(R.color.color_426FFF) : context.getColor(R.color.color_8D9AA9));
            myViewHolder.binding.item.setOnClickListener(view -> {
                if (null != onTabItemClickListener) {
                    onTabItemClickListener.onTabItemClickListener(position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        ItemTabBinding binding;

        public MyViewHolder(ItemTabBinding itemTabBinding) {
            super(itemTabBinding.getRoot());
            binding = itemTabBinding;
        }
    }

    public void setOnTabItemClickListener(OnTabItemClickListener onTabItemClickListener) {
        this.onTabItemClickListener = onTabItemClickListener;
    }

    public interface OnTabItemClickListener {
        void onTabItemClickListener(int position);
    }
}
